package de.lgohlke.serializer.kryo;

import java.math.BigDecimal;
import java.nio.ByteBuffer;

import com.esotericsoftware.kryo.serialize.SimpleSerializer;

/**
 * <p>BigDecimalSerializer class.</p>
 *
 * @author lars
 * @version $Id: $
 */
public final class BigDecimalSerializer extends SimpleSerializer<BigDecimal>
{

	/** {@inheritDoc} */
	@Override
	public void write(ByteBuffer buffer, BigDecimal object)
	{
		int scale = object.scale();
		BigDecimal scaledBD = object.scaleByPowerOfTen(scale);
		buffer.putInt(scaledBD.intValue());
		buffer.putInt(scale);
	}

	/** {@inheritDoc} */
	@Override
	public BigDecimal read(ByteBuffer buffer)
	{
		int bdI = buffer.getInt();
		int scale = buffer.getInt();
		BigDecimal bd = new BigDecimal(bdI);
		return bd.scaleByPowerOfTen(-scale);
	}

}
